var express = require('express');
var navItemClass = require('../routes/module/navItemClass');

var router = express.Router();

// 主导航栏 mainNav
let leftList = [
    { text:   "番剧", link:   "/", sty:    "" },
    { text:   "游戏中心", link:   "/", sty:    "" },
    { text:   "直播", link:   "/", sty:    "" },
    { text:   "会员购", link:   "/", sty:    "" },
    { text:   "漫画", link:   "/", sty:    "" },
    { text:   "特别栏", link:   "/", sty:    "" },
    { text:   "下载APP", link:   "/", sty:    "" },
];
let rightList = [
    { text:   "用户中心", link:   "/", sty:    "" },
    { text:   "大会员", link:   "/", sty:    "" },
    { text:   "消息", link:   "/", sty:    "" },
    { text:   "动态", link:   "/", sty:    "" },
    { text:   "收藏", link:   "/", sty:    "" },
    { text:   "历史", link:   "/", sty:    "" },
    { text:   "创作中心", link:   "/", sty:    "" },
    { text:   "投稿", link:   "/", sty:    "" },
];
let loginFlag = true;

// 公告栏内容
let placard = [
    "公告栏内容(oﾟvﾟ)ノ",
];

// 子导航栏 NavItems
let imgNavItems = [
    { num:  "", link:   "/", text:  "动态", fontClass: "fa-lightbulb-o" },
    { num:  "", link:   "/", text:  "排行榜", fontClass: "fa-bar-chart" },
    { num:  "", link:   "/", text:  "专区", fontClass: "fa-list-ol" },
];
let labelNavItems = [
    { num:  "999", link:   "/", text:  "动画", fontClass: "" },
    { num:  "999", link:   "/", text:  "音乐", fontClass: "" },
    { num:  "999", link:   "/", text:  "舞蹈", fontClass: "" },
    { num:  "999", link:   "/", text:  "知识", fontClass: "" },
    { num:  "999", link:   "/", text:  "生活", fontClass: "" },
    { num:  "233", link:   "/", text:  "时尚", fontClass: "" },
    { num:  "999", link:   "/", text:  "娱乐", fontClass: "" },
    { num:  "999", link:   "/", text:  "电影", fontClass: "" },
    { num:  "287", link:   "/", text:  "萌宠", fontClass: "" },
    { num:  "75", link:   "/", text:  "游戏", fontClass: "" },
    { num:  "999", link:   "/", text:  "记录", fontClass: "" },
    { num:  "453", link:   "/", text:  "数码", fontClass: "" },
    { num:  "132", link:   "/", text:  "科技", fontClass: "" },
    { num:  "114", link:   "/", text:  "资讯", fontClass: "" },
    { num:  "514", link:   "/", text:  "美食", fontClass: "" },
    { num:  "3", link:   "/", text:  "更多", fontClass: "fa-chevron-right" },
];
let addNavItems = [
    { num:  "", link:   "/", text:  "活动", fontClass: "fa-flag-checkered" },
    { num:  "", link:   "/", text:  "音乐盒", fontClass: "fa-music" },
    { num:  "", link:   "/", text:  "小黑屋", fontClass: "fa-balance-scale" },
    { num:  "", link:   "/", text:  "论坛", fontClass: "fa-commenting-o" },
];

// 轮播图对象列表


// 视频类型对象列表
let specialObject = {
    videosLineList: [
        [
            { img: "", link: "/player?id=1", title: "1", author: "", videoType: "", videoTime: "", likeVolue: "", authorImg: "", recommend: "", watchVolue: "", watchPeople: "", introduction: "", id: "1"},
            { img: "", link: "/player?id=2", title: "2", author: "", videoType: "", videoTime: "", likeVolue: "", authorImg: "", recommend: "", watchVolue: "", watchPeople: "", introduction: "", id: "2"},
            { img: "videoImg.webp", link: "/player?id=3", title: "3", author: "", videoType: "", videoTime: "", likeVolue: "", authorImg: "", recommend: "", watchVolue: "", watchPeople: "", introduction: "", id: "3"},
            { img: "videoImg.webp", link: "/player?id=4", title: "4", author: "", videoType: "", videoTime: "", likeVolue: "", authorImg: "", recommend: "", watchVolue: "", watchPeople: "", introduction: "", id: "4"},
        ],
        [
            { img: "", link: "/player?id=1", title: "1", author: "", videoType: "", videoTime: "", likeVolue: "", authorImg: "", recommend: "", watchVolue: "", watchPeople: "", introduction: "", id: "1"},
            { img: "", link: "/player?id=2", title: "2", author: "", videoType: "", videoTime: "", likeVolue: "", authorImg: "", recommend: "", watchVolue: "", watchPeople: "", introduction: "", id: "2"},
            { img: "videoImg.webp", link: "/player?id=3", title: "3", author: "", videoType: "", videoTime: "", likeVolue: "", authorImg: "", recommend: "", watchVolue: "", watchPeople: "", introduction: "", id: "3"},
            { img: "videoImg.webp", link: "/player?id=4", title: "4", author: "", videoType: "", videoTime: "", likeVolue: "", authorImg: "", recommend: "", watchVolue: "", watchPeople: "", introduction: "", id: "4"},
        ],
    ],
    type: "special"
};
let defaultObject = {
    videosLineList: [
        [
            { img: "", link: "/player?id=1", title: "1", author: "", videoType: "", videoTime: "", likeVolue: "", authorImg: "", recommend: "", watchVolue: "", watchPeople: "", introduction: "", id: "1"},
            { img: "", link: "/player?id=2", title: "2", author: "", videoType: "", videoTime: "", likeVolue: "", authorImg: "", recommend: "", watchVolue: "", watchPeople: "", introduction: "", id: "2"},
            { img: "videoImg.webp", link: "/", title: "3", author: "", videoType: "", videoTime: "02:31", likeVolue: "2341", authorImg: "", recommend: "", watchVolue: "10.1k", watchPeople: "", introduction: ""},
            { img: "videoImg.webp", link: "/", title: "4", author: "", videoType: "", videoTime: "02:31", likeVolue: "2341", authorImg: "authorImg.webp", recommend: "【ha】", watchVolue: "10.1k", watchPeople: "", introduction: ""},
        ]
    ],
    type: "default"
};
let imageObject = {
    videosLineList: [
        [
            { img: "", link: "/", title: "1", author: "", videoType: "", videoTime: "", likeVolue: "", authorImg: "authorImg.webp", recommend: "", watchVolue: "", watchPeople: "", introduction: ""},
            { img: "", link: "/", title: "2", author: "", videoType: "", videoTime: "", likeVolue: "", authorImg: "authorImg.webp", recommend: "", watchVolue: "", watchPeople: "", introduction: ""},
            { img: "videoImg.webp", link: "/", title: "3", author: "", videoType: "test", videoTime: "", likeVolue: "authorImg.webp", authorImg: "", recommend: "", watchVolue: "", watchPeople: "", introduction: "gu"},
            { img: "videoImg.webp", link: "/", title: "4", author: "KindSadan", videoType: "test", videoTime: "", likeVolue: "", authorImg: "authorImg.webp", recommend: "", watchVolue: "", watchPeople: "", introduction: "咕咕咕"},
        ]
    ],
    type: "image"
};

// 页尾 footer
let about = [
    { text:   "关于我们", link:   "/", sty:    "" },
    { text:   "加入我们", link:   "/", sty:    "" },
    { text:   "实名认证", link:   "/", sty:    "" },
    { text:   "联系我们", link:   "/", sty:    "" },
    { text:   "友情链接", link:   "/", sty:    "" },
    { text:   "特别感谢", link:   "/", sty:    "" },
    { text:   "用户协议", link:   "/", sty:    "" },
    { text:   "隐私政策", link:   "/", sty:    "" },
];
let send = [
    { text:   "帮助中心", link:   "/", sty:    "" },
    { text:   "侵权申述", link:   "/", sty:    "" },
    { text:   "精美壁纸", link:   "/", sty:    "" },
    { text:   "技术论坛", link:   "/", sty:    "" },
    { text:   "活动中心", link:   "/", sty:    "" },
    { text:   "广告合作", link:   "/", sty:    "" },
    { text:   "用户反馈", link:   "/", sty:    "" },
];
let imgSend = [
    { num:  "", link:   "/", text:  "客户端下载", fontClass: "fa-download" },
    { num:  "", link:   "/", text:  "新浪微博", fontClass: "fa-weibo" },
    { num:  "", link:   "/", text:  "官方微信", fontClass: "fa-weixin" },
];

// 在文件顶部添加模型引用
const Carousel = require('../models/Carousel');

// 修改首页路由
router.get('/', async (req, res) => {
  try {
    const carouselList = await Carousel.find().sort({ order: 1 }).limit(3);
    const mainNav = {
      leftList: [],
      rightList: [],
      loginFlag: false
    };

    const NavItems = {
      imgNavItems,
      labelNavItems,
      addNavItems
    };
    
    res.render('index', {
      title: '首页',
      carouselList,
      mainNav,
      placard: placard,
      NavItems,
      img: "",
      link: "",
      footer: {
        about,
        send,
        imgSend
      },
      videoObjectList: {
        specialObject: specialObject,
        defaultObject: defaultObject,
        imageObject: imageObject
      }
    });
  } catch (err) {
    console.error('Database query error:', err);
    res.status(500).send('服务器错误');
  }
});

// 添加媒体播放路由
router.get('/player', async (req, res) => {
  try {
    const mediaId = req.query.id;
    if (!mediaId) {
      return res.render('player', {
        title: '视频播放'
      });
    }
    
    const media = await Carousel.findById(mediaId);
    if (!media) {
      return res.status(404).send('未找到该媒体文件');
    }
    
    res.render('player', {
      title: '视频播放',
      mediaUrl: media.imageUrl,
      mediaId: media._id
    });
  } catch (err) {
    console.error('数据库查询错误:', err);
    res.status(500).send('服务器错误');
  }
});

module.exports = router;
